package com.xhd.file;

import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xhd.base.log.Log;
import com.xhd.base.log.LogFactory;
import com.xhd.server.ServerConfig;
import com.xhd.servlet.ServletMgr;

public class FileDownLoadServlet extends HttpServlet
{
  private static final long jm51 = 1L;
  private static HashMap jm279 = null;
  private boolean jm280 = false;
  private static String jm281 = null;
  private static int jm282 = 268435456;
  private static final Log jm9;
  static Class jm10;

  static
  {
    Class tmp16_13 = jm10;
    if (tmp16_13 == null)
    {
  
      try
      {
        FileDownLoadServlet.jm10 = Class.forName("com.sinocc.base.file.FileDownLoadServlet");
      }
      catch (ClassNotFoundException localClassNotFoundException)
      {
        throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
      }
    }
    jm9 = LogFactory.getLog(tmp16_13);
  }

  public void init()
    throws ServletException
  {
    super.init();
    jm9.info("Init Servlet: " + getServletName());
    jm279 = new HashMap(8);
    jm279.put(".doc", "application/msword");
    jm279.put(".xls", "application/vnd.ms-excel");
    jm279.put(".ppt", "application/vnd.ms-powerpoint");
    jm279.put(".txt", "text/plain");
    jm279.put(".jpg", "image/jpeg");
    jm279.put(".gif", "image/gif");
    jm279.put(".png", "image/png");
    String str1 = getInitParameter("is-view-image");
    this.jm280 = ((str1 != null) && (str1.equals("true")));
    str1 = getInitParameter("file-name-charset");
    if ((str1 != null) && (str1.length() > 0))
      jm281 = str1;
    str1 = getInitParameter("size-max");
    if ((str1 != null) && (str1.length() > 0))
      jm282 = Integer.parseInt(str1);
    String str2 = getInitParameter("cache-path");
    if ((str2 != null) && (str2.length() > 0))
    {
      if (ServerConfig.FILE_SEPARATOR != str2.charAt(str2.length() - 1))
        str2 = str2 + ServerConfig.FILE_SEPARATOR;
      XDecompressedFileCache.createCache(str2);
      str2 = getInitParameter("cache-max");
      if ((str2 != null) && (str2.length() > 0))
        XDecompressedFileCache.getInstance().setMax(Integer.parseInt(str2));
    }
    ServletMgr.getInstance().registerServlet(this);
  }

  protected void doGet(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws ServletException, IOException
  {
    process(paramHttpServletRequest, paramHttpServletResponse);
  }

  protected void doPost(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws ServletException, IOException
  {
    process(paramHttpServletRequest, paramHttpServletResponse);
  }

  protected void process(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
    throws ServletException, IOException
  {
    String str = paramHttpServletRequest.getParameter("filecode");
    if ((str == null) || (str.length() < 1) || ('*' != str.charAt(0)))
    {
      paramHttpServletResponse.sendError(401, "^_^");
      return;
    }
    FileDownloadCode localFileDownloadCode = FileDownloadCode.getInstance(str);
    if ((localFileDownloadCode == null) || (localFileDownloadCode.FilePath == null) || (localFileDownloadCode.DocName == null))
    {
      paramHttpServletResponse.sendError(401, "^_^");
      jm9.warn("Warn FileDownLoadServlet invaild filecode: " + str);
      return;
    }
    try
    {
      int i = ServletMgr.getInstance().servletBegin(this, paramHttpServletRequest);
      if (200 != i)
      {
        paramHttpServletResponse.sendError(i, "Servlet error!");
        return;
      }
      jm277(localFileDownloadCode, paramHttpServletResponse);
    }
    finally
    {
      ServletMgr.getInstance().servletEnd(this, paramHttpServletRequest);
    }
  }

  private void jm277(FileDownloadCode paramFileDownloadCode, HttpServletResponse paramHttpServletResponse)
    throws IOException
  {
    File localFile = new File(paramFileDownloadCode.FilePath);
    if (!(localFile.exists()))
    {
      paramHttpServletResponse.sendError(404, "no file");
      return;
    }
    if ((jm282 > 0) && (localFile.length() > jm282))
      paramHttpServletResponse.sendError(400, "Out of size: " + jm282);
    Object localObject1 = null;
    try
    {
      int i = 0;
      String str1 = "";
      int j = paramFileDownloadCode.DocName.lastIndexOf(46);
      if (j > 0)
        str1 = paramFileDownloadCode.DocName.substring(j).toLowerCase();
      String str2 = (String)jm279.get(str1);
      if (str2 == null)
        str2 = "application/x-msdownload";
      paramHttpServletResponse.setContentType(str2);
      if ((!(this.jm280)) || (!(str2.startsWith("image/"))))
        paramHttpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + jm278(paramHttpServletResponse, paramFileDownloadCode.DocName));
      if (paramFileDownloadCode.IsZiped == 2)
      {
        if (XDecompressedFileCache.canCache())
        {
          localObject2 = XDecompressedFileCache.getInstance().getFile(paramFileDownloadCode.FilePath, paramFileDownloadCode.FileTime);
          if (localObject2 == null)
            localObject2 = XDecompressedFileCache.getInstance().cacheFile(paramFileDownloadCode.FilePath, paramFileDownloadCode.FileTime);
          if (localObject2 != null)
          {
            localObject3 = new File((String)localObject2);
            i = (int)((File)localObject3).length();
            localObject1 = new FileInputStream((String)localObject2);
          }
        }
        if (localObject1 == null)
        {
          localObject2 = CPCCompressedFile.getInstance();
          localObject3 = new ByteArrayOutputStream();
          ((CPCCompressedFile)localObject2).decompressFile(paramFileDownloadCode.FilePath, (OutputStream)localObject3);
          i = ((ByteArrayOutputStream)localObject3).size();
          localObject1 = new ByteArrayInputStream(((ByteArrayOutputStream)localObject3).toByteArray());
          localObject3 = null;
        }
      }
      else if (paramFileDownloadCode.IsZiped != 3)
      {
        paramHttpServletResponse.setContentLength((int)localFile.length());
        localObject1 = new FileInputStream(localFile);
        i = (int)localFile.length();
      }
      if ((jm282 > 0) && (i > jm282))
        paramHttpServletResponse.sendError(400, "Out of size: " + jm282);
      if (i >= 33554432)
        jm9.warn("Warn FileDownLoadServlet file tool large: " + i);
      Object localObject2 = new byte[32768];
      paramHttpServletResponse.setContentLength(i);
      Object localObject3 = paramHttpServletResponse.getOutputStream();
      while (true)
      {
        i = ((InputStream)localObject1).read(localObject2);
        if (i >= 0)
        {
          if (i <= 0)
            continue;
          ((OutputStream)localObject3).write(localObject2, 0, i);
          ((OutputStream)localObject3).flush();
        }
      }
    }
    catch (Throwable localThrowable)
    {
      jm9.error(localThrowable);
      paramHttpServletResponse.sendError(500, "read file error.");
    }
    finally
    {
      if (localObject1 != null)
        try
        {
          ((InputStream)localObject1).close();
        }
        catch (Exception localException)
        {
        }
    }
  }

  private String jm278(HttpServletResponse paramHttpServletResponse, String paramString)
    throws UnsupportedEncodingException
  {
    byte[] arrayOfByte;
    if (jm281 == null)
      arrayOfByte = paramString.getBytes();
    else
      arrayOfByte = paramString.getBytes(jm281);
    return new String(arrayOfByte, paramHttpServletResponse.getCharacterEncoding());
  }
}
